<template>
  <div class="wh100">
    <el-form label-position="top" ref="form" :model="form" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="角色名称" prop="name" :rules="[
      {
        required: true,
        message: '角色名称不能为空',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.name" :disabled="modeType == 2"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="角色备注" prop="remark" :rules="[
      { required: true, message: '角色备注不能为空', trigger: 'blur' },
    ]">
            <el-input v-model="form.remark" :disabled="modeType == 2"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="权限id" prop="menuIds" :rules="[
      { required: true, message: '权限id不能为空', trigger: 'blur' },
    ]">
            <el-input v-model="form.menuIds" :disabled="true" @click.native="openAuthModel()"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="排序" prop="sort" :rules="[
      { required: true, message: '排序不能为空', trigger: 'blur' },
    ]">
            <el-input :min="1" v-model="form.sort" type="number" :disabled="modeType == 2"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="状态" prop="isDisable" required>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-switch v-model="form.isDisable" :disabled="modeType == 2"></el-switch>
                <span class="left10">启用</span>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>

        <el-col :span="24" style="text-align: right" v-if="roleInfo.type !== 2">
          <el-form-item>
            <div class="btn">
              <div class="cancel">
                <el-button @click="cancelModel()">取消</el-button>
              </div>
              <div class="preservation">

                <el-button type="primary" :loading="loading" @click="onSubmit('form')">保存</el-button>
              </div>
            </div>

          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-dialog :append-to-body="true" title="权限赋予" :visible.sync="dialogVisible">
      <authority-component @onClose="closeAuthModel" v-if="dialogVisible"></authority-component></el-dialog>
  </div>
</template>

<script>
import AuthorityComponent from "./AuthorityComponent.vue";
import { addRole, editRole } from "@/api/role";
export default {
  props: {
    modeType: {
      type: Number,
      default: 0,
    },
    roleInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  components: { AuthorityComponent },
  data() {
    return {
      form: {
        isDisable: false,
        name: "",
        menuIds: "1,2,3",
        remark: "",
        sort: null,
      },
      loading: false,
      dialogVisible: false,
    };
  },

  mounted() {
    if (this.modeType != 1) {
      this.form = JSON.parse(JSON.stringify(this.roleInfo));
      this.form.menuIds = "1,2,3",
      this.form.isDisable = this.form.isDisable == 1 ? true : false;
    }
  },

  methods: {
    closeAuthModel(e) {
      this.dialogVisible = false;
      if (e.menuIds.length > 0) {
        this.form.menuIds = e.menuIds.join(",");
      }
    },

    openAuthModel() {
      console.log("dddd");
      this.dialogVisible = true;
    },

    async addRoles() {
      let res = await addRole({
        isDisable: this.form.isDisable ? 1 : 0,
        name: this.form.name,
        menuIds: this.form.menuIds,
        remark: this.form.remark,
        sort: this.form.sort,
      });

      if (res.code == 200) {
        this.$message({
          message: "添加成功",
          type: "success",
        });
        this.loading = true;
        this.$emit("onClose", {
          isClose: true,
        });
      }
    },

    async editRoles() {
      let res = await editRole({
        id: this.roleInfo.id,
        name: this.form.name,
        menuIds: this.form.menuIds,
        remark: this.form.remark,
        sort: this.form.sort,
        isDisable: this.form.isDisable ? 1 : 0,
      });

      if (res.code == 200) {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.loading = true;
        this.$emit("onClose", {
          isClose: true,
        });
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.modeType == 1) {
            this.addRoles();
          } else if (this.modeType == 3) {
            this.editRoles();
          }
        }
      });
    },
    cancelModel() {
      this.$emit("onClose", {
        isClose: false,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.left10 {
  margin-left: 10px;
}

.btn {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-top: 110px;

  .cancel {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 0);
      color: rgba(92, 251, 255, 1);
    }
  }

  .preservation {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 1);
      color: rgba(23, 24, 26, 1);
    }
  }
}
</style>